﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using IAM.Interface.Model.ENUM;
using System.Web;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 日志记录
    /// </summary>
    public class Log : AbstractConvertObject,IConvertXml
    {
        #region 属性
        private string _serialno = "";
        private DateTime _time = System.DateTime.Now;
        private LogType _type = LogType.Information;
        private LogOperation _operation = LogOperation.LoginOK;
        private string _appaccount = "";
        private string _ipaddr = "";
        private string _note = "";
        /// <summary>
        /// 日志序号，集成系统中必须唯一
        /// </summary>
        public string SerialNo
        {
            get
            {
                return _serialno;
            }
            set
            {
                _serialno = value;
            }
        }
        /// <summary>
        /// 发生时间
        /// </summary>
        public DateTime Time
        {
            get
            {
                return _time;
            }
            set
            {
                _time = value;
            }
        }
        /// <summary>
        /// 日志类型
        /// </summary>
        public LogType Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }
        /// <summary>
        /// 操作类型
        /// </summary>
        public LogOperation Operation
        {
            get
            {
                return _operation;
            }
            set
            {
                _operation = value;
            }
        }
        /// <summary>
        /// 包括用于操作的应用系统账号信息
        /// </summary>
        public string AppAccount
        {
            get
            {
                return _appaccount;
            }
            set
            {
                _appaccount = value;
            }
        }
        /// <summary>
        /// 客户端IP地址
        /// </summary>
        public string IPAddr
        {
            get
            {
                return _ipaddr;
            }
            set
            {
                _ipaddr = value;
            }
        }
        /// <summary>
        /// 备注信息
        /// </summary>
        public string Note
        {
            get
            {
                return _note;
            }
            set
            {
                _note = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <Log ");
                _xml.Append(" SerialNo=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.SerialNo.ToString() )+ "\"");
                _xml.Append(" Time=\"" + this.Time.ToString() + "\"");
                _xml.Append(" Type=\"" + this.Type.ToString() + "\"");
                _xml.Append(" Operation=\"" + this.Operation.ToString() + "\"");
                _xml.Append(" AppAccount=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.AppAccount.ToString()) + "\"");
                _xml.Append(" IPAddr=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.IPAddr.ToString() )+ "\"");
                _xml.Append(" Note=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Note.ToString()) + "\"");
               _xml.Append(" />");
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            //1.获取当前结点
            this.SerialNo = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["SerialNo"].InnerText);
            this.Time = System.Convert.ToDateTime(this.CurrentXmlNode.Attributes["Time"].InnerText);
            string strType = this.CurrentXmlNode.Attributes["Type"].InnerText;
            switch (strType.ToLower())
            {
                case "information":
                    this.Type = LogType.Information;
                    break;
                case "warning":
                    this.Type = LogType.Warning;
                    break;
                case "error":
                    this.Type = LogType.Error;
                    break;

            }
            string operation = this.CurrentXmlNode.Attributes["Operation"].InnerText;
            switch (operation.ToLower())
            {
                case "loginok":
                    this.Operation = LogOperation.LoginOK;
                    break;
                case "loginerror":
                    this.Operation = LogOperation.LoginError;
                    break;
                case "logout":
                    this.Operation = LogOperation.Logout;
                    break;
            }
            this.AppAccount = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["AppAccount"].InnerText);
            this.IPAddr = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["IPAddr"].InnerText);
            this.Note = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Note"].InnerText);
            return this;
        }
        #endregion
    }
}
